简介
Cargo 是 Rust 的构建系统和包管理工具,可以帮助构建代码、下载依赖库、构建依赖库等。
安装 Rust 的时候会默认安装 Cargo,可以通过 cargo --version 判断是否安装
1 2
| $ cargo --version cargo 1.57.0 (b2e52d7ca 2021-10-21)
|
创建项目
使用 cargo new <path> 创建一个工程项目,工程项目名为 <path>。同时,也会创建一个 <path> 的目录,在不显示声明 vcs 的情况下,默认会同时创建 git 仓库,可以通过 cargo new <path> --vcs xxx 指定其他 vcs,或者通过 --vcs none 不创建 vcs。
1 2 3 4 5 6 7 8 9 10
| $ cargo new hello-world Created binary (application) `hello-world` package $ tree -a hello-world hello-world ├── .git ├── .gitignore ├── Cargo.toml └── src └── main.rs
|
- src 目录用于存放源代码
- Cargo.toml 是项目的配置文件,类似于 Golang 的 go.mod 文件
- .gitignore 中包含忽略 /target 的信息
Cargo.toml
1 2 3 4 5 6 7 8
| [package] name = "hello-world" version = "0.1.0" edition = "2021"
[dependencies]
|
- package 用于描述项目信息的
- name — 项目名
- version — 项目版本
- authors — 开发者昵称和邮箱信息,如果系统中存在
- edtion — 使用的 Rust 版本
- dependecies 用于描述项目的依赖项,在 Rust 中代码的包(库)称为
crate
转换为 Cargo
如果创建项目时没有使用 Cargo,也可以把项目转化为使用 Cargo,具体做法为:把源代码移到 src 目录下,创建 Cargo.toml 并填写相应的配置。
构建项目
满足 Cargo 规范的项目,可以使用 cargo build 来构建项目。
cargo build
dev
unoptimized + debuginfo
在项目的根目录下,执行 cargo build 会将可执行文件编译在 target/debug/hello-world 目录下,第一次构建时,会在顶层目录生成 cargo.lock 文件,负责追踪项目依赖的精确版本,类似于 Golang 的 go.sum 文件。
cargo.lock
1 2 3 4 5 6 7
|
version = 3
[[package]] name = "hello-world" version = "0.1.0"
|
1 2 3
| $ cargo build Compiling hello-world v0.1.0 (/Users/shenxianghong/Documents/Project/Rustaceans/hello-world) Finished dev [unoptimized + debuginfo] target(s) in 2.21s
|
release
optimized
默认情况,cargo build 适用于开发阶段的常规编译,在最终发布构建时,可以通过 cargo build --release 发布,这样在编译的时候会进行优化,代码运行的更快,但是编译的时间也更长,同时,会在 target/release 而不是 target/debug 生成可执行文件。
1 2 3
| $ cargo build --release Compiling hello-world v0.1.0 (/Users/shenxianghong/Documents/Project/Rustaceans/hello-world) Finished release [optimized] target(s) in 0.38s
|
cargo run
cargo run 本质上就是编译代码 + 执行结果,如果之前编译成功过,且源码没有改变,那么就会直接运行二进制文件。
1 2 3 4 5 6 7 8 9 10
| $ cargo run Compiling hello-world v0.1.0 (/Users/shenxianghong/Documents/Project/Rustaceans/hello-world) Finished dev [unoptimized + debuginfo] target(s) in 0.66s Running `target/debug/hello-world` Hello, world!
$ cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/hello-world` Hello, world!
|
cargo check
cargo check 用于检查代码,确保能通过编译,但是不会实质的进行编译,cargo check 运行速度比 cargo build 快得多,可以用于开发阶段反复检查,提高效率。
1 2 3
| $ cargo check Checking hello-world v0.1.0 (/Users/shenxianghong/Documents/Project/Rustaceans/hello-world) Finished dev [unoptimized + debuginfo] target(s) in 0.06s
|
当检查有错误时,会报错提示
1 2 3 4 5 6 7 8 9
| $ cargo check Checking hello-world v0.1.0 (/Users/shenxianghong/Documents/Project/Rustaceans/hello-world) error: expected one of `->`, `;`, `where`, or `{`, found `err` --> src/main.rs:1:11 | 1 | fn main() err { | ^^^ expected one of `->`, `;`, `where`, or `{`
error: could not compile `hello-world` due to previous error
|
国内源加速
默认情况下,cargo 获取包依赖等通过 crate.io,由于网络问题,可以将其替换成国内的 cargo 源。
~/.cargo/config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = 'sjtu'
[source.tuna] registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
[source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index"
[source.sjtu] registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
[source.rustcc] registry = "git://crates.rustcc.cn/crates.io-index"
|